<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        $(document).ready(function () {
            $('#loginForm').on('submit', function (e) {
                e.preventDefault(); // 阻止表单默认提交行为


                var data = {
                    "userCode": "B06224",
                    "userPwd": "123456"
                }

                var body = {
                    "signPhone": "4c35629ea6a5c608",
                    "signType": "手动打卡",
                    "longitude": "113.888312",
                    "groupSetId": "adc32b0ea6f84c3cb0f0e76d70d6b96d",
                    "signWIFIName": "BS-office",
                    "latitude": "22.501162",
                    "type": "签退",
                    "signWIFI": "94:14:57:86:27:fb"
                }


                console.log("序列化表单数据," + JSON.stringify(data));


                $.ajax({
                    type: "POST",
                    url: "https://erp.bosum.com/qa/erp/user/login",
                    contentType: "application/json",  // 指定请求的Content-Type为application/json
                    //dataType: 'application/json;charset=UTF-8',
                    //           headers: {
                    //               "content-type": "application/json;charset=utf-8",
                    //                token: getCookie("token"),
                    //            },
                    async: false,
                    data: JSON.stringify(data),
                    success: function (res) {
                        console.log(res);
                        var cookies = xhr.getResponseHeader('Set-Cookie'); // 获取响应头中的cookie
                        console.log(cookies);
                        SetCookie("token", res.data.token);
                        SetCookie("userinfo", res.data);
                    },
                    error: function (err) {
                        console.log("err");
                        console.log(err);
                    },
                });

                // $.ajax({
                //     type: "POST",
                //     url: "https://erp.bosum.com/qa/erp/phone/attendanceSign/sign",
                //     contentType: "application/json",  // 指定请求的Content-Type为application/json
                //     dataType: 'application/json;charset=UTF-8',
                //     headers: {
                //         "content-type": "application/json;charset=utf-8",
                //         "Cookie": "SESSION=9427467c-397e-4f40-ac71-5e4380b8e200; Path=/qa/erp; HttpOnly;"
                //     },
                //     async: false,
                //     data: JSON.stringify(body),
                //     success: function (res) {
                //         console.log(res);
                //         if (res.code == "1") {
                //             // 保存cookie
                //             SetCookie("token", res.data.token);
                //             SetCookie("userinfo", res.data);
                //             console.log(res);
                //         } else {
                //             alert(res.msg);
                //         }
                //     },
                //     error: function (err) {
                //         console.log(err);
                //     },
                // });


            });
        });

        //写cookies函数
        function SetCookie(name, value) {
            //两个参数，一个是cookie的名子，一个是值
            var Days = 7; //此 cookie 将被保存 7 天
            var exp = new Date(); //new Date("December 31, 9998");
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
            document.cookie =
                name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        }

        function getCookie(name) {
            //取cookies函数
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (arr != null) return unescape(arr[2]);
            return null;
        }

        function delCookie(name) {
            //删除cookie
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = getCookie(name);
            if (cval != null)
                document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        }
    </script>
</head>
<body>

<form id="loginForm">
    <label>
        <input type="text" name="userCode" id="userCode" placeholder="Username" value="B06224">
    </label>
    <label>
        <input type="password" name="userPwd" id="userPwd" placeholder="Password" value="123456">
    </label>
    <button type="submit">Login</button>
</form>

</body>
</html>